package com.thientan.controller

import java.text.SimpleDateFormat
import java.util.Map;

import com.thientan.model.HopDong
import com.thientan.service.InKhachHangService
import com.thientan.utils.Constants
import com.thientan.utils.Utility

class InKhachHangController extends HopDongController{
	InKhachHangService inKhachHangService
	
	@Override
	public Object getDomainService() {
		return inKhachHangService
	}
	
	@Override
	protected String getExportFileName(){
		"PhieuThuKhachHang"
	}
	
	@Override
	protected Map buildExportData(Map exportParams){
		String barcodeBasePath = servletContext.getRealPath("") + File.separator;
		String barcodePath = barcodeBasePath + Constants.BARCODE_FILE_PATH
		Long hopDongId = params['id'] as Long
		// Collect hopDong infor to export Phieu Thu
		HopDong hopDong = HopDong.get(hopDongId)
		String maHopDong = hopDong?.maHd ?: ""
		// Generate barcode
		Utility.generateBarcode(maHopDong, barcodePath)
		// Prepare data for export
		String maNv1 = hopDong?.nvBan1?.maNv ?: ""
		return ['data' : [],
				'ngayin' : new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT).format(Calendar.getInstance().getTime()),
				'tenkhachhang' : hopDong?.khachHang.hoTen,
				'diachi' : hopDong?.khachHang.fullDiaChi,
				'dienthoai' : hopDong?.khachHang.dienThoai,
				'doitruong' : hopDong?.nvDoiTruong?.maNv,
				'nv' : (maNv1 ? maNv1 + " + " : "") + (hopDong?.nvBan2?.maNv ?: ""),
				'conlai' : Utility.moneyFormat(hopDong.giaTri - hopDong.tienTraTruoc),
				'mavachpath' : barcodePath,
				'logopath' : barcodeBasePath + Constants.LOGO_FILE_PATH
				]
		
		
	}
}
